package cn.veliger.swagger2;

import cn.veliger.swagger2.properties.Swagger2Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author veliger
 * @create 2018-06-03 11:55
 * @desc
 **/
@Configuration
@EnableSwagger2
public class Swagger2AutoConfiguration {
  @Autowired
  private Swagger2Properties swagger2Properties;
  @Bean
  @ConfigurationProperties(prefix = "cn.veliger.swagger2")
  public Swagger2Properties swagger2Properties(){
    return new Swagger2Properties();
  }
  @Bean
  public Docket createRestApi() {
    Docket docket = new Docket(DocumentationType.SWAGGER_2);
    docket = docket.apiInfo(apiInfo());
    if (!StringUtils.isEmpty(swagger2Properties.getGroupName())) {
      docket = docket.groupName(swagger2Properties.getGroupName());
    }
    ApiSelectorBuilder apiSelectorBuilder = docket.select();
    if (!StringUtils.isEmpty(swagger2Properties.getBasePackage())) {
      apiSelectorBuilder = apiSelectorBuilder.apis(RequestHandlerSelectors.basePackage(swagger2Properties.getBasePackage()));
    }
    if (!StringUtils.isEmpty(swagger2Properties.getAntPath())) {
      apiSelectorBuilder = apiSelectorBuilder.paths(PathSelectors.ant(swagger2Properties.getAntPath()));
    } else {
      apiSelectorBuilder = apiSelectorBuilder.paths(PathSelectors.any());
    }

    return apiSelectorBuilder.build();
  }

  private ApiInfo apiInfo() {
    ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
    if (!StringUtils.isEmpty(swagger2Properties.getTitle())) {
      apiInfoBuilder = apiInfoBuilder.title(swagger2Properties.getTitle());
    }
    if (!StringUtils.isEmpty(swagger2Properties.getDescription())) {
      apiInfoBuilder = apiInfoBuilder.description(swagger2Properties.getDescription());
    }
    if (!StringUtils.isEmpty(swagger2Properties.getTermsOfServiceUrl())) {
      apiInfoBuilder = apiInfoBuilder.termsOfServiceUrl(swagger2Properties.getTermsOfServiceUrl());
    }
    apiInfoBuilder.contact(new Contact(swagger2Properties.getContactName(), swagger2Properties.getContactUrl(), swagger2Properties.getContactEmail()));
    if (!StringUtils.isEmpty(swagger2Properties.getVersion())) {
      apiInfoBuilder = apiInfoBuilder.version(swagger2Properties.getVersion());
    } else {
      apiInfoBuilder = apiInfoBuilder.version("1.0.0");
    }
    return apiInfoBuilder.build();
  }
}

